<template>
  <!--  完成-->
  <div style="width: 80%">
    <div style="margin-bottom: 30px">编辑用户</div>
    <el-form :inline="true" :model="form" label-width="100px">
      <el-form-item label="id">
        <el-input v-model="form.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="学号">
        <el-input v-model="form.studentId" disabled></el-input>
      </el-form-item>
      <el-form-item label="专业">
        <el-input v-model="form.major" placeholder="请输入专业"></el-input>
      </el-form-item>
      <el-form-item label="年级">
        <el-input v-model="form.grade" disabled></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="dialogVisible = true" size="medium">修改密码</el-button>
      <el-button type="primary" @click="save" size="medium">提交</el-button>
      <el-button @click="cancel" size="medium">取消</el-button>
    </div>

    <el-dialog title="修改密码" :visible.sync="dialogVisible" width="20%">
      <el-form :model="information" :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="密码:" prop="password">
          <el-input v-model="information.password" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="savePassword">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import md5 from 'js-md5'
export default {
  name: 'EditUser',
  data() {
    return {
      form: {},
      dialogVisible: false,
      information: {},
      rules: {
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }]
      }
    }
  },
  created() {
    const id = this.$route.query.id
    request.get("/user/" + id).then(res => {
      this.form = res.data
    })
  },
  methods: {
    save() {
      request.put('/user/update', this.form).then(res => {
        if (res.code === '200') {
          this.$notify.success('更新成功')
          this.$router.push("/user")
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    cancel() {
      this.$router.push('/user')
    },
    savePassword() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form))
          let password = md5(this.information.password + 'qingge')
          data.password = password
          console.log(data);
          request.put(`/user/update`, data).then(res => {
            if (res.code === '200') {
              this.dialogVisible = false
              this.$message({
                message: '修改成功',
                type: 'success'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}

</script>

